import axios from 'axios'
import router from '@/router/index'
import { showNotify } from 'vant'
import store from '@/store'

const url = import.meta.env.MODE === 'dev' ? '' : import.meta.env.VITE_API_URL

console.log(import.meta.env)

axios.interceptors.request.use(
  (config) => {
    config.url = url + config.url
    if (config.method == 'post') {
      config.headers['Content-Type'] = 'application/json'
      config.headers['Authorization'] = store.userStore.token
      // config.url = url + config.url
    } else if (config.method == 'upload') {
      // config.url = url + config.url
      config.method = 'post'
      // const file = config.data;
      // const formData = new FormData();
      // config.data = formData.append('file', file);
    } else {
      config.method = 'get'
      config.headers['Content-Type'] = 'application/json'
      config.headers['Authorization'] = store.userApp.token
      // config.url = url + config.url
      if (config.data) {
        const keys = Object.keys(config.data)
        keys.map((v) => {
          config.url =
            config.url + (config.url.includes('?') ? '&' : '?') + v + '=' + config.data[v]
        })
        if (!config.url.includes('t=')) {
          config.url =
            config.url + (config.url.includes('?') ? '&' : '?') + 't=' + new Date().getTime()
        }
      }
    }
    // if (store.state['common'].token) {
    //   config.headers['Authorization'] = store.state['common'].token
    //     ? store.state['common'].token
    //     : '';
    // }
    // for (const key in sign) {
    //   config.headers[key] = sign[key];
    // }
    return config
  },
  function (error) {
    console.log(error)
    return Promise.reject(error)
  }
)

axios.interceptors.response.use(
  (response) => {
    if (response.data.code === 401) {
      showNotify({
        type: 'danger',
        message: '您的登录信息已过期，请重新登录!'
      })
      // store.userStore.LoginOut()
      // router.push('/login')
      return Promise.reject(response.data)
    }

    if (response.request.responseType === 'blob') {
      return Promise.resolve(response)
    }

    if (response.data.code !== 0) {
      showNotify({
        type: 'danger',
        message: response.data.msg
      })
      // loading.close();
      return Promise.reject(response.data)
    }
    return Promise.resolve(response.data)
  },
  function (error) {
    if (!error.response.data.msg) {
      showNotify({
        type: 'danger',
        message: '系统异常,请稍后再试'
      })
      return Promise.reject(error)
    }
    // if (error.response.status === 401) {
    //   message.error('您的登陆信息已过期')
    //   store.userStore.setUserInfo({})
    //   router.push({
    //     name: 'Login'
    //   })
    //   return Promise.reject(error)
    // }
    showNotify({
      type: 'danger',
      message: error.response.data.msg
    })
    return Promise.reject(error.response.data)
  }
)

function post(options) {
  return axios({ ...options, method: 'post' })
}

function get(options) {
  return axios({ ...options, method: 'get' })
}

function upload(options) {
  return axios({ ...options, method: 'upload' })
}

export default {
  post: post,
  upload: upload,
  get: get
}
